﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Jerkymon.Cryptography
{
    public class Generic
    {
		#region Static Methods (6)
		public static string Decrypt(string pass)
        {
            return Decrypt(pass, string.Empty);
        }

		private static string Decrypt(string strText, string strEncrKey)
        {
            byte[] bKey = new byte[8];
            byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

            try
            {
                bKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8));
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                Byte[] inputByteArray = inputByteArray = Convert.FromBase64String(strText);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(bKey, IV), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                System.Text.Encoding encoding = System.Text.Encoding.UTF8;
                return encoding.GetString(ms.ToArray());
            }
            catch (Exception ex)
            {
                string estr = ex.ToString();
                return "";
            }
        }

		public static string Encrypt(string pass)
        {
            return Encrypt(pass, string.Empty);
        }

		private static string Encrypt(string strText, string strEncrKey)
        {
            byte[] bKey = new byte[8];
            byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

            try
            {
                bKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8));
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                Byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(bKey, IV), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                return Convert.ToBase64String(ms.ToArray());

            }
            catch (Exception ex)
            {
                string estr = ex.ToString();
                return "";
            }
        }

		/// <summary>
        /// to use in encrypt 
        /// Security.MEncrypt(datahere)
        /// ----
        /// to use in decrypt
        /// Security.MEncrypt(datahere)
        /// </summary>
        /// <param name="strValue"></param>
        /// <returns></returns>
        public static string Reverse(string strValue)
        {
            return genNewString(strValue);
        }

		private static string genNewString(string strValue)
        {
            int i, j, k;
            string strNewStringValue = "";
            char[] chrValue = strValue.ToCharArray();

            for (i = 0; i <= chrValue.Length - 1; i++)
            {
                // take the next character in the string
                j = (int)chrValue[i];

                // find out the character code
                k = (int)j;

                if (k >= 97 && k <= 109)
                {
                    // a ... m inclusive become n ... z
                    k = k + 13;
                }
                else if (k >= 110 && k <= 122)
                {
                    // n ... z inclusive become a ... m
                    k = k - 13;
                }
                else if (k >= 65 && k <= 77)
                {
                    // A ... m inclusive become n ... z
                    k = k + 13;
                }
                else if (k >= 78 && k <= 90)
                {
                    // N ... Z inclusive become A ... M
                    k = k - 13;
                }

                //add the current character to the string returned by the function
                strNewStringValue = strNewStringValue + (char)k;
            }

            return strNewStringValue;
        }
		#endregion
    }
}
